﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
using System.Data;

namespace DAO
{
    public class ChiTietHoaDonDAO
    {
        public static int ThemChiTietHoaDon(ChiTietHoaDonDTO dv)
        {
            string sql;
            sql = "Insert into ChiTietHoaDon(MaChiTietHoaDon,MaHoaDon,MaSach,SoLuongBan,DonGia) values ('" + dv.MaChiTietHoaDon + "','" + dv.HoaDon.MaHoaDon + "','" + dv.Sach.MaSach + "'," + dv.SoLuongBan + "," + dv.DonGiaBan + ")";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }

        public static ChiTietHoaDonDTO LayThongTinCTHoaDon(String ma)
        {
            ChiTietHoaDonDTO cthd = new ChiTietHoaDonDTO();
            string sql = "Select * From ChiTietHoaDon where MaChiTietHoaDon = '" + ma + "'";
            DataTable dt = DataAccess.ExcuQuery(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                cthd.MaChiTietHoaDon = Convert.ToString(dt.Rows[0]["MaChiTietHoaDon"]);
                cthd.DonGiaBan = float.Parse(dt.Rows[0]["DonGia"].ToString());
                cthd.HoaDon = HoaDonDAO.LayThongTinHoaDon(dt.Rows[0]["MaHoaDon"].ToString());
                cthd.Sach = SachDAO.LayThongTinSach(dt.Rows[0]["MaSach"].ToString());
                cthd.SoLuongBan = Int32.Parse(dt.Rows[0]["SoLuongBan"].ToString());
            }
            return cthd;
        }

        public static int CapNhat(ChiTietHoaDonDTO cthd)
        {
            string sql;
            sql = "Update ChiTietHoaDon Set MaHoaDon = '" + cthd.HoaDon.MaHoaDon + "', MaSach= '" + cthd.Sach.MaSach + "', SoLuongBan=" + cthd.SoLuongBan + ", DonGia= " + cthd.DonGiaBan + " where MaChiTietHoaDon = '" + cthd.MaChiTietHoaDon + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }

        public static int Xoa(String ma)
        {
            string sql;
            sql = "Update ChiTietHoaDon Set TrangThai= 0 where MaChiTietHoaDon = '" + ma + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }

        public static ArrayList LayDanhSachTheoHoaDon(String mahd)
        {
            ArrayList arr = new ArrayList();
            string sql;
            sql = "Select * From ChiTietHoaDon where TrangThai=1 and MaHoaDon= '" + mahd + "' ";
            DataTable dt = DataAccess.ExcuQuery(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ChiTietHoaDonDTO dv = new ChiTietHoaDonDTO();
                dv.MaChiTietHoaDon = Convert.ToString(dt.Rows[i]["MaChiTietHoaDon"]);
                dv.DonGiaBan = float.Parse(dt.Rows[i]["DonGiaBan"].ToString());
                dv.HoaDon = HoaDonDAO.LayThongTinHoaDon(dt.Rows[i]["MaHoaDon"].ToString());
                dv.Sach = SachDAO.LayThongTinSach(dt.Rows[i]["MaSach"].ToString());
                dv.SoLuongBan = Int32.Parse(dt.Rows[i]["SoLuongBan"].ToString());
                arr.Add(dv);
            }
            return arr;
        }
    }
}
